﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileCopyer.CompareFiles
{
    public partial class CompareFilesForm : Form
    {
        public CompareFilesForm()
        {
            InitializeComponent();
        }

        private void CompareFilesForm_Load(object sender, EventArgs e)
        {
            var dir1 = new DirectoryInfo(GlobalConfig.Instance.BariumFileName + @"\packages\engine\Source");
            var dir2 = new DirectoryInfo(GlobalConfig.Instance.CesiumFilePath + @"\packages\engine\Source");

            var f1 = dir1.GetFiles("*.js", SearchOption.AllDirectories).ToList();
            var f2 = dir2.GetFiles("*.js", SearchOption.AllDirectories).ToList();

            Dictionary<string, string> f1d = new Dictionary<string, string>();
            Dictionary<string, string> f2d = new Dictionary<string, string>();
            for (var i = 0; i < f1.Count; i++)
            {
                var fiName = f1[i].Name;
                if (f1[i].FullName.IndexOf("\\Shaders\\") > 0)
                {
                    fiName = "[s]" + fiName;
                }
                f1d.Add(fiName, f1[i].FullName);
            }
            for (var i = 0; i < f2.Count; i++)
            {
                var fiName = f2[i].Name;
                if (f2[i].FullName.IndexOf("\\Shaders\\") > 0)
                {
                    fiName = "[s]" + fiName;
                }
                if (f2d.ContainsKey(fiName))
                {
                    continue;
                }
                f2d.Add(fiName, f2[i].FullName);
            }

            var l1 = GlobalConfig.Instance.BariumFileName.Length;
            var l2 = GlobalConfig.Instance.BariumFileName.Length;

            Dictionary<string, string> refineFiles = new Dictionary<string, string>();
            refineFiles.Add("World.js", "Globe.js");

            for (var i = 0; i < f1.Count; i++)
            {
                var tname = f1[i].Name;
                if (refineFiles.ContainsKey(f1[i].Name))
                {
                    tname = refineFiles[f1[i].Name];
                }
                var f2Info = f2d.ContainsKey(tname) ? f2d[tname] : null;

                ListViewItem lvi = new ListViewItem(f1[i].Name);
                lvi.SubItems.Add(f1[i].FullName.Substring(l1));
                lvi.SubItems.Add(f2Info == null ? "" : f2Info.Substring(l2));
                var len1 = f1[i].Length;
                var lne2 = f2Info == null ? 0 : new FileInfo(f2Info).Length;

                if (len1 == lne2) { continue; }

                lvi.SubItems.Add(len1.ToString());
                lvi.SubItems.Add(lne2.ToString());

                lvi.Tag = new string[]
                {
                    f1[i].FullName,
                    f2Info
                };
                if (IsContentSame(lvi.Tag as string[])) { continue; }

                this.listView1.Items.Add(lvi);
            }
        }

        private bool IsContentSame(string[] files)
        {
            string[] line1, line2;
            if (File.Exists(files[0]))
            {
                var ts = File.ReadAllLines(files[0]);
                line1 = ts;
            }
            else
            {
                return false;
            }
            if (line1[0] == "//same as source file")
            {
                return true;
            }
            if (string.IsNullOrEmpty(files[1]) == false)
            {
                var ts = File.ReadAllLines(files[1]);
                line2 = ts;
            }
            else
            {
                return false;
            }
            if (line1.Length != line2.Length) { return false; }

            for (var i = 0; i < line1.Length; i++)
            {
                if (line1[i].Trim() != line2[i].Trim())
                {
                    return false;
                }
            }
            return true;
        }


        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count == 0) { return; }
            var selItem = this.listView1.SelectedItems[0];

            var files = selItem.Tag as string[];

            textBox3.Text = files[0];
            textBox4.Text = files[1];


            if (File.Exists(files[0]))
            {
                var ts = File.ReadAllLines(files[0]);
                StringBuilder sb = new StringBuilder();
                for (var i = 0; i < ts.Length; i++)
                {
                    sb.AppendLine(ts[i]);
                }
                this.textBox1.Text = sb.ToString();
            }
            if (string.IsNullOrEmpty(files[1]) == false)
            {
                var ts = File.ReadAllLines(files[1]);
                StringBuilder sb = new StringBuilder();
                for (var i = 0; i < ts.Length; i++)
                {
                    sb.AppendLine(ts[i]);
                }
                this.textBox2.Text = sb.ToString();
            }
        }

        private void textBox3_MouseClick(object sender, MouseEventArgs e)
        {
            Clipboard.SetText(textBox3.Text);
        }

        private void textBox4_MouseClick(object sender, MouseEventArgs e)
        {
            Clipboard.SetText(textBox4.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var allLines = File.ReadAllLines(textBox3.Text);
            List<string> sb = new List<string>();
            sb.Add("//same as source file");
            sb.AddRange(allLines);
            File.WriteAllLines(textBox3.Text, sb.ToArray());

            listView1_SelectedIndexChanged(null, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.listView1.Items.Clear();
            CompareFilesForm_Load(null, null);
        }
    }
}
